<?php

class ZendLuceneSearch
{

  //start: Zend_Lucene_Search
  static public function getLuceneIndex($index_name = null)
  {
//    ProjectConfiguration::registerZend();
    require_once 'Zend/Search/Lucene.php';
    if (file_exists($index = self::getLuceneIndexFile($index_name)))
    {
      return Zend_Search_Lucene::open($index);
    }

    return Zend_Search_Lucene::create($index);
  }

  static public function getLuceneIndexFile($index_name = null)
  {
    return sfConfig::get('sf_data_dir').'/lucene_index/'.$index_name;
  }

  static public function getLuceneIndexForQuery($query, $index_name)
  {
    if ($query)
    {
        $hits = self::getLuceneIndex($index_name)->find($query);

        $pks = array();
        foreach ($hits as $hit)
        {
          $pks[] = $hit->pk;
        }

        if (empty($pks))
        {
          return array();
        }

        return $pks;
    }
    else
    {
        return null;
    }
  }

  static public function deleteLuceneIndex($index = null, $id = null)
  {
    if ($index === null && $id == null)
    {
        return;
    }
    else
    {
        foreach ($index->find('pk:'.$id) as $hit)
        {
          $index->delete($hit->id);
        }
    }
  }
  //end: Zend_Lucene_Search

}

?>
